package com.snap.vseries.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.dialog.NumberInputDialog;

import java.math.BigDecimal;

import butterknife.BindViews;

@SuppressLint("ValidFragment")
public class InnerCrossFragment extends BaseFragment {

    @BindViews({R.id.tv_1_1, R.id.tv_1_2, R.id.tv_1_3, R.id.tv_1_4,
            R.id.tv_2_1, R.id.tv_2_2, R.id.tv_2_3, R.id.tv_2_4,
            R.id.tv_3_1, R.id.tv_3_2, R.id.tv_3_3, R.id.tv_3_4,
            R.id.tv_4_1, R.id.tv_4_2, R.id.tv_4_3, R.id.tv_4_4})
    TextView[] tvs;

    private ExpInfo experimentInfo;
    private double[] cross;

    public InnerCrossFragment(ExpInfo experimentInfo) {
        this.experimentInfo = experimentInfo;
        this.cross = experimentInfo.getCrosstalk();
    }

    @Override
    protected int layoutResId() {
        return R.layout.dialog_inner_cross;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        for (int i = 0; i < cross.length; i++) {
            final int index = i;
            tvs[i].setText(String.valueOf(cross[i]));
            if (i % 5 == 0) continue;
            tvs[i].setOnClickListener(v ->
                    new NumberInputDialog()
                            .setParam(3, 0, Float.parseFloat(tvs[index].getText().toString()), 0.1f, "荧光串扰值", "",
                                    value -> {
                                        BigDecimal b = new BigDecimal(String.valueOf(value));
                                        tvs[index].setText(String.valueOf(b.doubleValue()));
                                    })
                            .setMaxLength(5)
                            .show(getChildFragmentManager(), ""));
        }
    }

    public void apply() {
        for (int i = 0; i < cross.length; i++) {
            if (i % 5 == 0) continue;
            String value = tvs[i].getText().toString();
            BigDecimal b = new BigDecimal(value);
            cross[i] = b.doubleValue();
        }
    }
}